﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Samples.Location;

namespace _4SquareLite
{
    public partial class frmGPS : Form
    {
        private EventHandler updateDataHandler;
        GpsDeviceState device = null;
        GpsPosition position = null;

        Gps gps = new Gps();

        public frmGPS()
        {
            InitializeComponent();
            Program.arrForm.Add(this);
            this.Text = Program.strProgramName;
        }

        private void inputPanel1_EnabledChanged(object sender, EventArgs e)
        {
            if (inputPanel1.Enabled)
            {
                pnlForm.Height -= inputPanel1.Bounds.Height;
            }
            else
            {
                pnlForm.Height = this.Height;
            }
        }

        private void frmGPS_Load(object sender, EventArgs e)
        {
            // GPS Stuff
            updateDataHandler = new EventHandler(UpdateData);
            gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);

            lblLat.Text = "";
            lblLon.Text = "";

            btnTip.Enabled = false;
            btnVenue.Enabled = false;
            miRefresh.Enabled = false;
        }

        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            position = args.Position;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            Invoke(updateDataHandler);
        }

        void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
        {
            device = args.DeviceState;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            Invoke(updateDataHandler);
        }

        void UpdateData(object sender, System.EventArgs args)
        {
            if (gps.Opened)
            {
                //string str = "";
                //if (device != null)
                //{
                //    str = device.FriendlyName + " " + device.ServiceState + ", " + device.DeviceState + "\n";
                //}

                if (position != null)
                {
                    if (position.LatitudeValid)
                    {
                        //str += "Latitude (DD):\n   " + position.Latitude + "\n";
                        //str += "Latitude (D,M,S):\n   " + position.LatitudeInDegreesMinutesSeconds + "\n";
                        lblLat.Text = position.Latitude.ToString();
                    }

                    if (position.LongitudeValid)
                    {
                        //str += "Longitude (DD):\n   " + position.Longitude + "\n";
                        //str += "Longitude (D,M,S):\n   " + position.LongitudeInDegreesMinutesSeconds + "\n";
                        lblLon.Text = position.Longitude.ToString();
                    }

                    if ((position.LatitudeValid) && (position.LongitudeValid))
                    {
                        btnTip.Enabled = true;
                        btnVenue.Enabled = true;
                        miRefresh.Enabled = true;

                        
                    }

                    if (position.SatellitesInSolutionValid &&
                        position.SatellitesInViewValid &&
                        position.SatelliteCountValid)
                    {
                        //str += "Satellite Count:\n   " + position.GetSatellitesInSolution().Length + "/" +
                        //    position.GetSatellitesInView().Length + " (" +
                        //    position.SatelliteCount + ")\n";

                        pbSignal.Maximum = position.GetSatellitesInView().Length;
                        pbSignal.Value = position.GetSatellitesInSolution().Length;
                    }

                    //if (position.TimeValid)
                    //{
                    //    str += "Time:\n   " + position.Time.ToString() + "\n";
                    //}
                }
            }
        }

        private void miBack_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }

            int i = Program.arrForm.Count;
            i--;
            Program.arrForm.RemoveAt(i);
            i--;
            ((Form)Program.arrForm[i]).Show();
            this.Close();
        }

        private void btnVenue_Click(object sender, EventArgs e)
        {
            frmVenueList oForm = new frmVenueList(lblLat.Text, lblLon.Text, "Your Location");
            oForm.Show();
            this.Hide();
        }

        private void btnTip_Click(object sender, EventArgs e)
        {
            frmTipList oForm = new frmTipList(lblLat.Text, lblLon.Text, "Your Location");
            oForm.Show();
            this.Hide();
        }

        private void btnSwitch_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
                btnSwitch.Text = "On";
                pbSignal.Value = 0;
            }
            else
            {
                gps.Open();
                btnSwitch.Text = "Off";
                lblLat.Text = "...searching...";
                lblLon.Text = "...searching...";
            }
        }

        private void miRefresh_Click(object sender, EventArgs e)
        {
            string strURL = Program.getGoogleMapURL(lblLat.Text, lblLon.Text, pbMap.Size, true);
            pbMap.Image = Program.getImageFromURL(strURL);
        }
    }
}